using Microsoft.EntityFrameworkCore;
using UniversalAdmin.Domain.Entities;
using UniversalAdmin.Domain.Repositories;
using UniversalAdmin.Infrastructure.Data.Contexts;

namespace UniversalAdmin.Infrastructure.Repositories;

public class SystemConfigRepository : Repository<SystemConfig>, ISystemConfigRepository
{
    public SystemConfigRepository(ApplicationDbContext context) : base(context)
    {
    }

    public async Task<SystemConfig?> GetByKeyAsync(string key)
    {
        return await _context.SystemConfigs
            .FirstOrDefaultAsync(c => c.Key == key);
    }

    public async Task DeleteAsync(string key)
    {
        var config = await _context.SystemConfigs
            .FirstOrDefaultAsync(c => c.Key == key);

        if (config != null)
        {
            _context.SystemConfigs.Remove(config);
            await _context.SaveChangesAsync();
        }
    }
}